package com.csu.service.serviceImpl;

import com.csu.entity.ExamGrade;
import com.csu.mapper.ExamGradeMapper;
import com.csu.mapper.ParentStudentMapper;
import com.csu.mapper.SubjectMapper;
import com.csu.mapper.UserMapper;
import com.csu.service.ExamGradeService;
import com.csu.vo.ExamGradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamGradeServiceImpl implements ExamGradeService {

    @Autowired
    private ExamGradeMapper examGradeMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ParentStudentMapper parentStudentMapper;

    //查询所有学生的成绩信息
    @Override
    public List<ExamGradeVo> getAllGradeOfStudent(int examId, int user_id) {
        //先通过老师user_id找到学生user_id的List，以及自己的subject_id， 然后在exam_grade表中根据user_id, subject_id和exam_id找到examGrades
        List<ExamGrade> examGradeList = new ArrayList<>();
        int classId = userMapper.getClassIdByUserId(user_id);
        List<Integer> userIdList = userMapper.getUserIdListByClassId(classId);
        int subjectId = userMapper.getSubjectIdByUserId(user_id);
        System.out.println("userIdList:"+userIdList);

        for(Integer studentId: userIdList){
            System.out.println("studentId:"+studentId);
            ExamGrade examGrade = examGradeMapper.getExamGradeByExamIdAndUserIdAndSubjectId(examId, studentId, subjectId);
            if (examGrade != null)
                examGradeList.add(examGrade);
        }
        //返回列表对象
        List<ExamGradeVo> examGradeVos = new ArrayList<>();
        for (ExamGrade examGrade : examGradeList){
            //进行类型转换
            ExamGradeVo examGradeVo = ExamGradeToExamGradeVo(examGrade);
            examGradeVos.add(examGradeVo);
        }
        return examGradeVos;
    }

    //查询一个学生的成绩信息
    @Override
    public List<ExamGradeVo> getOneGradeOfStudent(int exam_id, int user_id) {
        //返回列表对象
        List<ExamGradeVo> examGradeVos = new ArrayList<>();
        int roleId = userMapper.getRoleIdByUserId(user_id);
        if (roleId == 2){
            List<Integer> studentIds = parentStudentMapper.getChildrenIdList(user_id);
            for (Integer studentId : studentIds){
                //从数据库中通过ExamId和UserId查询记录
                List<ExamGrade> examGrades =  examGradeMapper.getExamGradeByExamIdAndUserId(exam_id,studentId);
                for (ExamGrade examGrade : examGrades){
                    //进行类型转换
                    ExamGradeVo examGradeVo = ExamGradeToExamGradeVo(examGrade);
                    examGradeVos.add(examGradeVo);
                }
            }
        }else{
            //从数据库中通过ExamId和UserId查询记录
            List<ExamGrade> examGrades =  examGradeMapper.getExamGradeByExamIdAndUserId(exam_id,user_id);
            for (ExamGrade examGrade : examGrades){

                //进行类型转换
                ExamGradeVo examGradeVo = ExamGradeToExamGradeVo(examGrade);
                examGradeVos.add(examGradeVo);
            }
        }
        return examGradeVos;
    }

    //把ExamGrade转化为ExamGradeVo
    public ExamGradeVo ExamGradeToExamGradeVo(ExamGrade examGrade){
        ExamGradeVo examGradeVo = new ExamGradeVo();
        examGradeVo.setGradeId(examGrade.getGradeId());
        examGradeVo.setExamId(examGrade.getExamId());
        examGradeVo.setGrade(examGrade.getGrade());
        examGradeVo.setSubjectName(subjectMapper.getSubjectNameByID(examGrade.getSubjectId()));
        examGradeVo.setUsername(userMapper.getUsernameById(examGrade.getUserId()));

        return examGradeVo;
    }
}
